package com.ehotting.edsta.circuitDesign.config;

import com.ehotting.eaf.core.DateHelper;
import com.ehotting.eaf.core.utils.ip.LocalIPHelper;
import com.ehotting.edsta.mqbd.api.enums.SysModuleEnum;
import com.ehotting.mqbd.api.bean.solver.SolverInfoBean;
import com.ehotting.mqbd.api.bean.solver.SolverStateBean;
import com.ehotting.mqbd.core.manager.NacosSolverManager;
import com.ehotting.mqbd.core.manager.SolverInfoManager;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
@Configuration
@ConfigurationProperties
public class ApplicationContextListenerRedis implements CommandLineRunner, DisposableBean {

    private static Logger logger = LoggerFactory.getLogger(ApplicationContextListenerRedis.class);

    @Value("${server.port}")
    private String port;

    private SysModuleEnum module = SysModuleEnum.CIRCUIT_DESIGN;

    @Autowired
    private LocalIPHelper localIPHelper;
    @Autowired
    private SolverInfoManager solverInfoManager;
    @Autowired
    private NacosSolverManager nacosSolverManager;


    @Override
    public void run(String... args)  {
        logger.info("Start register");

        String ip = localIPHelper.getIp();
        logger.info("module[{}],ip[{}],port[{}]",module.getCode(),ip,port);

        SolverStateBean solverState = new SolverStateBean();
        SolverInfoBean solverInfo = new SolverInfoBean();
        solverInfo.setIp(ip);
        solverInfo.setPort(port);
        solverInfo.setName(module.getName()+":"+ DateHelper.toSeparatorStringyyyyMMddHHmmss(new Date()));
        solverState.setSolverInfo(solverInfo);
        solverState.setAvailable();
        solverState.setSolvingCount(0);
        solverState.setSolveTotal(10);
        solverInfoManager.save(solverState);
        try {
            nacosSolverManager.add(module.getCode(),solverInfo);
        } catch (JsonProcessingException e) {
            logger.error("Module["+module+",ip:"+ip+"] login error",e);
        }

        logger.info("Finish register");
    }

    @Override
    public void destroy() throws Exception {
        logger.info("Start destroy");

        String ip = localIPHelper.getIp();

        logger.info("module[{}],ip[{}],port[{}]",module.getCode(),ip,port);

        solverInfoManager.delete(module.getCode(),ip);

        SolverInfoBean solverInfo = new SolverInfoBean();
        solverInfo.setIp(ip);
        solverInfo.setPort(port);
        try {
            nacosSolverManager.delete(module.getCode(),solverInfo);
        } catch (JsonProcessingException e) {
            logger.error("Module["+module+",ip:"+ip+"] logout error",e);
        }

        logger.info("Finish destroy");
    }


}